package com.longnuoqi.mydoctor.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.longnuoqi.mydoctor.activity.ZizhenClassifyActivity;
import com.longnuoqi.mydoctor.adapter.ZizhenGridAdapter;
import com.yunhe.mydoctor.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Lenovo on 2015/12/27.
 */
public class ZizhenFragment extends Fragment {
    private EditText searchText;
    private ImageView searchBtn;

    private LinearLayout linearLayout;
    private ImageView more;

    private GridView gridView;
    private ZizhenGridAdapter gridAdapter;
    private final static int[] gvImg = {R.mipmap.gv1, R.mipmap.gv2, R.mipmap.gv3, R.mipmap.gv4,
            R.mipmap.gv5, R.mipmap.gv6, R.mipmap.gv7, R.mipmap.gv8};
    private final static String[] gvTxt = {"持续发烧", "月经失调", "气胸", "胃溃疡",
            "不孕不育", "鼻炎", "类风湿", "流行感冒"};

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.zizhen_layout, container, false);
        initView(view);
        initGridView(view);
        return view;
    }

    private void initView(View view) {
        searchText = (EditText) view.findViewById(R.id.zizhen_search_text);
        searchBtn = (ImageView) view.findViewById(R.id.zizhen_search_btn);
        linearLayout = (LinearLayout) view.findViewById(R.id.zizhen_linear);
        more = (ImageView) view.findViewById(R.id.zizhen_more);

        searchBtn.setOnClickListener(onClickListener);
        linearLayout.setOnClickListener(onClickListener);
        more.setOnClickListener(onClickListener);
    }

    private void initGridView(View view) {
        gridView = (GridView) view.findViewById(R.id.zizhen_gridview);
        List list = new ArrayList<>();
        for (int i = 0; i < gvImg.length; i++) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("img", gvImg[i]);
            map.put("name", gvTxt[i]);
            list.add(map);
        }
        gridAdapter = new ZizhenGridAdapter(getActivity(), list);
        gridView.setAdapter(gridAdapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getActivity(), "position:" + position, Toast.LENGTH_SHORT).show();
            }
        });

    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.zizhen_search_btn:
                    Toast.makeText(getActivity(), "搜索相关内容", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.zizhen_more:
                    Toast.makeText(getActivity(), "查看更多", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.zizhen_linear:
                    Intent intent = new Intent(getActivity(), ZizhenClassifyActivity.class);
                    startActivityForResult(intent,0);
                     break;
                default:
                    break;
            }
        }
    };

}
